﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using Common.Client.Lib.Extensions;
using Syncfusion.SfSkinManager;
using Syncfusion.Themes.FluentDark.WPF;
using Syncfusion.Themes.FluentLight.WPF;

namespace Syncfusion.Lib.Extensions
{
    public static class ThemeExtension
    {
        /// <summary>
        /// 设置主题
        /// </summary>
        /// <param name="dependencyObject"></param>
        /// <param name="themeName"></param>
        public static void SetTheme(
            this DependencyObject? dependencyObject,
            string themeName = "FluentLight")
        {
            IThemeSetting themeSetting = GetThemeSetting(themeName);
            var themeTypeName = themeSetting.GetType().Name.Replace("ThemeSettings", "");

            Syncfusion.SfSkinManager.SfSkinManager.RegisterTheme(themeTypeName, GetThemeHelper(themeName));
            Syncfusion.SfSkinManager.SfSkinManager.RegisterThemeSettings(themeTypeName, themeSetting);
            Syncfusion.SfSkinManager.SfSkinManager.SetTheme(dependencyObject, new Theme(themeName)
            {
                //ScrollBarMode = ScrollBarMode.Compact,//紧凑滚动条
                //FocusVisualKind = FocusVisualKind.HighVisibility,//高可见性键盘视觉反馈,当用户将键盘焦点移动到可聚焦元素时，会显示该元素的边框。
            });
            var theme = Syncfusion.SfSkinManager.SfSkinManager.GetTheme(dependencyObject);
            if (theme is FluentTheme fluentTheme)
            {
                //关闭亚克力背景
                //SkinManagerHelper.SetShowAcrylicBackground(dependencyObject, false);
                fluentTheme.ShowAcrylicBackground = false;
                
            }
            //Syncfusion.SfSkinManager.SfSkinManager.SetSizeMode(dependencyObject, SizeMode.Touch);// 触控兼容大小
        }

        private static IThemeSetting GetThemeSetting(string themeName)
        {
            FontFamily? fontFamily = "PrimaryFontFamily".GetResource<FontFamily>(new FontFamily("Arial,Microsoft YaHei UI"));
            //IThemeSetting theme = new FluentLightThemeSettings() { FontFamily = fontFamily };
            IThemeSetting theme = new FluentLightThemeSettings() {};
            switch (themeName)
            {
                case "FluentLight":
                    var fluentLightTheme = new FluentLightThemeSettings() { };
                    theme = fluentLightTheme;
                    break;
                case "FluentDark":
                    var fluentDarkTheme = new FluentDarkThemeSettings() { };
                    theme = fluentDarkTheme;
                    break;
            }
            return theme;
        }

        private static SkinHelper GetThemeHelper(string themeName)
        {
            FontFamily? fontFamily = "PrimaryFontFamily".GetResource<FontFamily>(new FontFamily("Arial,Microsoft YaHei UI"));
            //IThemeSetting theme = new FluentLightThemeSettings() { FontFamily = fontFamily };
            SkinHelper res = new FluentLightSkinHelper() { };
            switch (themeName)
            {
                case "FluentLight":
                    var fluentLightTheme = new FluentLightSkinHelper() { 
                    };
                    res = fluentLightTheme;
                    break;
                case "FluentDark":
                    var fluentDarkTheme = new FluentDarkSkinHelper() { };
                    res = fluentDarkTheme;
                    break;
            }
            return res;
        }
    }
}
